import gnu.trove.TIntArrayList;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;


public class RunProgram implements Runnable{

	private Robot robot;
	protected String fileName;
	private ArrayList<String> commands; 
	private TIntArrayList seconds;	//the time in which each command has to be executed
	
	private int time = 0; 			//time program has to run
	private int second = 0;     	//the second program is currently running at
	private int commandNumber = 0;	//number of next command to be executed
	private int amount = 0; 		//total amount of commands
	protected boolean done = false;   //if finished all commands in program
	
	static ProgramExecuter executer = null;
	
	//default c'tor
	//private RunProgram(){}
	//constructor
	public RunProgram(Robot r, String name, Boxes boxes){
		robot = r;
		fileName = name;
		r.hasFile();
		
		//initialize executer if needed
		if(executer == null)
			executer = new ProgramExecuter(boxes);
		
	}
	
	//Method creates list of commands from text file
	public void getCommands(){
		/*create list of commands - each command is made out of the second it should
		be executed and the command itself*/
		try{
				commands = new ArrayList<String>();
				seconds = new TIntArrayList();
				BufferedReader br = new BufferedReader( new FileReader(fileName) );
				
				String line;
				String[] command = new String[2];
				while((line = br.readLine()) != null){
					//split line into seconds and command
					command = line.split(" ", 2);
					commands.add(command[1]);
					seconds.add(Integer.parseInt(command[0]));
				}
				
				amount = commands.size();
		}
		catch(FileNotFoundException e){ System.out.println("Error: file not found.");}
		catch(IOException e){ System.out.println("Error in IO");}
	}
	
	//sets amount of time run will execute
	public void setTime(int time){ this.time += time; }

	//run function - runs commands from file on robot
	public void run(){
		  //get second of next command
		  int comSec = seconds.get(commandNumber);
		  //while time isn't up
		  try{
			  while(GUI.run() && commandNumber < amount){
				  //if there is a command to be executed at this second	
				  if(second == comSec){
					  	String cmd = commands.get(commandNumber); //get command
					  	System.out.format("\t %s on %s: %d %s\n" ,fileName,
	  							robot.getName(), comSec, cmd  );
					  	executer.runCommand(cmd, robot);//run command
				  		commandNumber++;
				  		//if isn't last command
				  		if(commandNumber < amount)
				  			comSec = seconds.get(commandNumber);
				  	}

				  	robot.act();
				 
				  	Thread.sleep(1000); //sleep for 1 second
				  	second++;
			  }
			  //if commands are finished
			  if(commandNumber == amount){
				  done = true;
				  //set robot to not have file
				  robot.setHasFile(false);
			  }
		  }catch(InterruptedException e){ System.out.println("thread interrupted");}
	}
	

	
}
